<?php

namespace Apps\Manage\Controllers;

use Apps\Common\Models\BeautyParlorTagInfo;
use Phalcon\Mvc\View;
use PhalconPlus\Common\ResponseResult;
use PhalconPlus\Common\ResponseResultStatus;
use Apps\Common\Models\BeautyParlorTag;
/**
 * 标签管理
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class BptagController extends BpBaseController
{
    public function initialize()
    {
        parent::initialize();
        $this->setMainNav('bp');
    }
    /**
     * 标签选择
     */
    public function layerAction()
    {
        $req = $this->request;
        $this->view->setVar('tags', $req->getQuery('tags', null, ''));
        $this->view->setVar('callback', $req->getQuery('callback', null, ''));

        $this->view->setVar('dataList', BeautyParlorTagInfo::query()
            ->columns('tag_id id, tag_title title')
            ->where('tag_state = 1 and parent_id != 0')
            ->execute()
        );
    }

    /**
     * 列表
     */
    public function listAction()
    {
        $this->setLeftNav('tag_list');
        $req=$this->request;
        $page = intval($req->getQuery('page', null, 1));
        $page = $page > 0 ? $page : 1;
        $limit = $this->pageNavLimit;
        $offset = $limit * ($page - 1);
        $list=BeautyParlorTagInfo::query()
        ->limit($limit,$offset)
        ->orderBy("tag_id desc")
        ->execute()->toArray();
        foreach ($list as $k=>$v){
        	if($list[$k]['parent_id'] != 0){
        		$tag_info=BeautyParlorTagInfo::find("tag_id = ".$list[$k]['parent_id'])->toArray();
        		$list[$k]['parent_name']=$tag_info[0]['tag_title'];
        	}
        }
        $total=BeautyParlorTagInfo::count();
        
        $this->view->setVar('total', $total);
        $this->view->setVar('page', $page);
        $this->view->setVar('limit', $limit);
        
        $this->view->setVar('data', $list);
    }

    /**
     * 标签是否存在
     * @param $name
     * @return bool
     */
    private function getTagIsExist($name)
    {
        return BeautyParlorTagInfo::count([
            'conditions' => 'tag_title = :title:',
            'bind' => ['title' => $name]
        ]) > 0;
    }
    /**
     * 添加
     * @return ResponseResult|void
     */
    public function addAction()
    {
        $this->setLeftNav('tag_add');
        $req = $this->request;
        if(!$req->isPost()) {
        	$list=BeautyParlorTagInfo::find("tag_state = 1 and parent_id = 0");
        	$this->view->setVar('list',$list);
            return;
        }

        $response = new ResponseResult();
        $response->callback = $req->getPost('callback', null, 'parent.setFormResult');
        $response->callbackJavascriptTag = true;
        $name = $req->getPost('tag_name');
		$parent_id = $req->getPost('parent_id');
        if(empty($name)) {
            $response->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '名称必须填写！');
            return $response;
        }
        // 查询是否存在了
        if($this->getTagIsExist($name)) {
            $response->sendError(ResponseResultStatus::PARAM_ERROR, '名称已经存在！');
            return $response;
        }
        $tag = new BeautyParlorTagInfo();
        $tag->tag_title = $name;
        $tag->parent_id=$parent_id;
        if($tag->save() == false) {
            $this->databaseErrorLog($tag);
            $response->sendError(ResponseResultStatus::DATABASE_ERROR, '保存数据异常！');
        } else {
            $response->sendResult($tag->tag_id);
        }
        return $response;
    }

    /**
     * 设置状态
     * @return $this|ResponseResult
     */
    public function setStateAction()
    {
        $req = $this->request;
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $state = intval($req->getPost('state', null, -10000));
            if(!in_array($state, [-1, 1])) {
                return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
            }
            $info = BeautyParlorTagInfo::findFirst('tag_id=' . $id);
            if($info) {
                $info->update(['tag_state' => $state]);
            }
            return (new ResponseResult())->sendResult('ok');
        } else {
            return (new ResponseResult())->sendError(ResponseResultStatus::PARAM_CANNOT_EMPTY, '请求异常');
        }
    }

    /**
     * 修改标签信息
     * @date: 2016年1月19日 
     * @author: futao
     */
    public function updateAction()
    {
        $req = $this->request;
        $echo = function($msg, $state=false){
        	echo '<script type="text/javascript">';
        	echo 'parent.formResult("' . $msg . '", ' . ($state ? 1 : 0) . ')';
        	echo '</script>';
        };
        if($req->isPost()) {
            $id = intval($req->getPost('id', null, 0));
            $title = $req->getPost('tag_name', null, null);
            $parent_id = intval($req->getPost('parent_id',null,0));
            if($id<0 || $parent_id<0){
            	$echo("请求异常");
            }
            if(empty($title)) {
            	$echo("请求异常");
            }
            // 查询是否存在了
            if($this->getTagIsExist($title)) {
                $echo("此标签名称已经存在！");
            }
            $info = BeautyParlorTagInfo::findFirst('tag_id=' . $id);
            if($info) {
                $info->update(['tag_title' => $title,'parent_id' => $parent_id]);
            }
            $echo("ok",true);
        } else if($req->getQuery('id')){
        	$id = intval($req->getQuery('id',null,0));
        	if($id<1){
        		echo "error!";
        		return;
        	}
        	$info =BeautyParlorTagInfo::findFirst("tag_id = $id and tag_state = 1");
        	if(!$info){
        		$echo("标签不存在或被删除！");
        		return;
        	}
        	$list=BeautyParlorTagInfo::find("tag_state = 1 and parent_id = 0");
        	$this->view->setVar('list',$list);
        	$this->view->setVar('info',$info);
            $this->view->pick('bptag/edit');
        }else{
        	$echo("请求异常");
        }
    }
}